layui.use(['table', 'admin', 'ax','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;

    laydate.render({
    	elem:'#startTime'
    });
    
    laydate.render({
    	elem:'#endTime'
    });

    laydate.render({
    	elem:'#startTime2'
    });
    
    laydate.render({
    	elem:'#endTime2'
    });

    laydate.render({
      elem: '#startTime3'
        	,value:new Date()
    });

    laydate.render({
      elem: '#endTime3'
        	,value:new Date()
    });

    var OutOrder = {
        tableId: "outOrderTable"
    };
	    
    /**
     * 初始化 出库订单
     */
    OutOrder.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键id'},
            {field: 'customerNumber', title: 'Customer Order No.', style:'color: #2b85e4;font-weight: 600;',width:150,event: 'detailedSign'},
            {field: 'outOrderStatus', sort: true, title: 'Document Status',templet: '#outOrderStatusTpl'},
            {field: 'setMentiontime', sort: true, title: 'Shipment Date'},
            {field: 'createUserName', title: 'Created By'},
            {field: 'getUnitId', title: 'Unloading Unit ID'},
            {field: 'kilometer', title: 'Kilometer'},
            {field: 'getUnit', title: 'Unloading Unit', style:'font-weight: 600;',width:180},
            {field: 'getLocation', title: 'Unloading Location',width:250},
            {field: 'outWay', title: 'Outbound Method'},
            {field: 'note', title: 'Remarks',width:120, totalRowText: 'Total:'},
            {field: 'sumVolume', title: 'Volume', totalRow: true},
            {field: 'sumNum', title: 'Quantity', totalRow: true},
            {field: 'sumMoney', title: 'Value', totalRow: true},
            {field: 'outCustomerName', title: 'Customer Name'},
            {field: 'storehouseName', title: 'Warehouse Name'},
            {field: 'outOrderNumber', sort: true, title: 'Outbound Order No.', style:'font-weight: 600;',event: 'detailedSign'},
            {field: 'createTime', sort: true, title: 'Creation Time'},
            {field: 'submitTime', sort: true, title: 'Submission Time'},
            {field: 'submitterName', title: 'Submitter'},
            {field: 'examineTime', sort: true, title: 'Confirmation Time'},
            {field: 'examineName', title: 'Confirmer'},
            {field: 'finishOutTime', sort: true, title: 'Outbound Completion Time'},
        ]];
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + OutOrder.tableId,
        url: Feng.ctxPath + '/outOrder/getQuerylist',
        page: true,
        limit: 60,
		limits:[40,60,120,240,360,480,1200],
    	height: "full-60",
        cellMinWidth: 100,
    	toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
        totalRow: true ,//开启合计行
        cols: OutOrder.initColumn()
    });

    /**
     * 点击查询按钮
     */
    OutOrder.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
    	queryData['outCustomerId'] = $("#outCustomerId").val();
        queryData['outStorehouseId'] = $("#outStorehouseId").val();
        queryData['outOrderStatus'] = $("#outOrderStatus").val();
        queryData['startTime'] = $("#startTime").val();
        queryData['endTime'] = $("#endTime").val();
        queryData['startTime2'] = $("#startTime2").val();
        queryData['endTime2'] = $("#endTime2").val();
        queryData['startTime3'] = $("#startTime3").val();
        queryData['endTime3'] = $("#endTime3").val();
        table.reload(OutOrder.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    $('#btnSearch').click(function () {
		OutOrder.search()
    });

    // 批量打印先飞点击事件
    $('#btnPrint').click(function () {
        var checkStatus = table.checkStatus('outOrderTable');
        data2 = checkStatus.data;
        if (data2.length>0) {
            var ids = [];
            for (var i = 0; i < data2.length; i++) {
                ids.push(data2[i].id);
            }
            layer.open({
                type: 2,
                title: 'Batch Print Outbound Orders',
                area: ['990px', '550px'],
                anim: 5,
                content: Feng.ctxPath + '/outOrder/print?ids='+ids,
                end: function () {
                }
            });
        } else {
            layer.alert('Please select at least one record');
        }
    });

    // 批量打印点击事件
    $('#btnPrints').click(function () {
        var checkStatus = table.checkStatus('outOrderTable');
        data2 = checkStatus.data;
        if (data2.length>0) {
            var ids = [];
            for (var i = 0; i < data2.length; i++) {
                ids.push(data2[i].id);
            }
            layer.open({
                type: 2,
                title: 'Batch Print Outbound Orders',
                area: ['990px', '550px'],
                anim: 5,
                content: Feng.ctxPath + '/outOrder/prints?ids='+ids,
                end: function () {
                }
            });
        } else {
            layer.alert('Please select at least one record');
        }
    });

    // 批量打印泉林交货单点击事件
    $('#btnPrintQljhd').click(function () {
        var checkStatus = table.checkStatus('outOrderTable');
        data2 = checkStatus.data;
        if (data2.length>0) {
            var ids = [];
            for (var i = 0; i < data2.length; i++) {
                ids.push(data2[i].id);
            }
            layer.open({
                type: 2,
                title: 'Batch Print Delivery Notes',
                area: ['990px', '550px'],
                anim: 5,
                content: Feng.ctxPath + "/outOrder/printQljhd?ids="+ids,
                end: function () {
                }
            });
        } else {
            layer.alert('Please select at least one record');
        }
    });

    // 工具条点击事件
    table.on('tool(' + OutOrder.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'detailedSign') {
            layer.open({
                type: 2,
                title: 'Order Details',
                area: ['1000px', '500px'],
                anim: 5,
                content: Feng.ctxPath + '/outOrderDetailed/toQuery?detailedOutNumber=' + data.outOrderNumber,
            });
        }
    });
});
